let express = require('express');
let router = express.Router();
let registerDao  = require('../../model/client/register')
let equipmentDao = require('../../model/backend/equipment');
//客户端初始化注册，服务器地址验证接口

/**
 * @api {get} /api/client/register/verify  服务器地址验证接口
 * @apiName 服务器地址验证接口
 * @apiGroup register
 * @apiVersion 0.1.0 
 * @apiSuccess {Int} status 状态码
 * @apiSuccess {String} msg  信息
 */
router.get('/verify', async function (req,res) {

    await res.json({status:200,msg:"验证成功！"})
    
})


//小区列表获取接口
/**  
* @api {get} /api/client/register/village/list  小区列表获取接口
* @apiName 小区列表获取接口
* @apiGroup register 
* @apiVersion 0.1.0
* @apiSuccess {Int} status 状态码
* @apiSuccess {String} msg  信息
* @apiSuccess {Json} data  数据

*/

router.get('/village/list',async function(req,res){
  let villageList  =await registerDao.getAllVillage();
  await res.json({status:200,msg:'success',data:villageList})
     
})
// 楼宇列表获取接口
/**  
* @api {get} /api/client/register/building/list/:villageId  小区楼宇获取接口
* @apiName 小区楼宇列表获取接口
* @apiGroup register 
* @apiVersion 0.1.0
* @apiParam {Number}  villageId  小区Id
* @apiSuccess {Int} status 状态码
* @apiSuccess {String} msg  信息
* @apiSuccess {Json} data  数据

*/
router.get('/building/list/:villageId' ,async function(req,res){
    let villageId = req.params.villageId;
    if(!villageId){
        return await res.json({status:203,msg:"请选择小区！"})
    }
    let getBuildingList  = await registerDao.getBuildingList(villageId);

    await res.json({status:200,msg:'success',data:getBuildingList})
})

//单元列表 获取接口

/**  
* @api {get} /api/client/register/unit/list/:buildingId  单元获取接口
* @apiName 单元获取接口
* @apiGroup register 
* @apiVersion 0.1.0
* @apiParam {Number}  buildingId   楼宇Id
* @apiSuccess {Int} status 状态码
* @apiSuccess {String} msg  信息
* @apiSuccess {Json} data  数据

*/

router.get('/unit/list/:buildingId' ,async function(req,res){
    let  buildingId  = req.params.buildingId;
    if(!buildingId){
        return await res.json({status:203,msg:'请选择楼宇！'})
    }
    let getUnitList  = await registerDao.getUnitList(buildingId);
    await res.json({status:200,msg:'success',data:getUnitList})
})



//注册接口

/**  
* @api {post} /api/client/register  设备注册接口
* @apiName 注册接口
* @apiGroup register 
* @apiVersion 0.1.0
* @apiParam {Number}  villageId  小区Id
* @apiParam {Number}  buildingId  楼宇Id
* @apiParam {Number}  unitId  单元Id
* @apiParam {String}  mac  mac 地址

* @apiSuccess {Int} status 状态码
* @apiSuccess {String} msg  信息
*/
router.post('/' ,async function(req,res){
    let villageId =req.body.villageId;
    let buildingId =req.body.buildingId;
    let unitId =req.body.unitId;
    let mac = req.body.mac;
    if(!villageId){
        return await res.json({status:203,msg:"请选择小区！"})
    }
    if(!buildingId){
        return await res.json({status:203,msg:"请选择所属楼！"})
    }
    if(!unitId){
        return await res.json({status:203,msg:"请选择所属单元！"})
    }
    if(!mac){
        return await res.json({status:203,msg:"请输入设备mac 地址！"})
    }

     //验证mac 地址的唯一性
     let valicationMac  = await registerDao.valicationMac(mac);
      if(valicationMac.length>0){
          return await res.json({status:403,msg:"此mac地址已被注册"})
      } 
    
      await registerDao.addEquipment(mac);
      await res.json({status:200,msg:'success'})

})


//组装所有小区楼宇 单元三级数据


/**  
* @api {get} /api/client/register/list  小区3级数据获取接口
* @apiName 小区3级数据获取接口
* @apiGroup register 
* @apiVersion 0.1.0
* @apiSuccess {Int} status 状态码
* @apiSuccess {String} msg  信息
* @apiSuccess {Json} data  数据

*/
router.get("/list" ,async function(req,res){
    let villageList  =await registerDao.getAllVillage();
    let  buildingList  =await  registerDao.buildingList();
    let  unitList   = await registerDao.unitList();
    //拼装楼宇与单元数据
    if(villageList.length>0){
     for (let index = 0; index < buildingList.length; index++) {
         let  building = buildingList[index];
              building.unitList=[];
         for (let a = 0; a < unitList.length; a++) {
             let unit = unitList[a];
             if(unit.buildingId === building.id){
                 building.unitList.push(unit)
             }
         }

     } 
     //拼接所有的楼宇与小区的数据
      for (let b = 0; b < villageList.length; b++) {
          let village = villageList[b];
              village.buildingList=[]; 
       for (let c = 0; c < buildingList.length; c++) {
           let element = buildingList[c];
           if(element.villageId===village.id){
            village.buildingList.push(element)
           }
           
       }       
      }     
    }
    let data =[];
    data = villageList; 
    await res.json({status:200,msg:'success',data:data})

})


module.exports =router;
